<?php

namespace app\admin\controller;

use think\Db;
class Menu extends Base
{
    //显示菜单
    public function index(){
        // echo 1;die;
        $data=Db::table('sys_menu')->where('status_id',1)->order('sort','desc')->select();
        // var_dump($data);
        foreach ($data as $key => $value) {
            # code...
            $data[$key]['name']=$value['menu_name'];
            $data[$key]['pId']=$value['parent_id'];
            // echo $value['pId'];die;
            unset($data[$key]['menu_name']);
            unset($data[$key]['parent_id']);
        }
        $this->assign('data', json_encode($data));
        return  $this->fetch();
    }
//    添加菜单行为
    public function  addMenuBehavior(){
        $data=$this->request->param();
        $res=Db::table('sys_menu')->strict(false)->insertGetId($data);
        if($res){
            return json(['msg'=>'ok','id'=>$res]);
        }else{
            return json(['msg'=>'fail']);
        }
    }
//    编辑菜单行为
    public function editMenuBehavior(int $id){
        $data=$this->request->param();
        $res=Db::table('sys_menu')->where('id', $id)->update($data);
        if($res){
            return json(['msg'=>'ok']);
        }else{
            return json(['msg'=>'fail']);
        }
    }
//    菜单删除
    public function delete(int $id){
        $res=Db::table('sys_menu')->delete($id);
        if($res){
            return json(['msg'=>'ok']);
        }else{
            return json(['msg'=>'fail']);
        }
    }
}
